<template>
  <div class="character">
    <el-col :span="24" class="action-bar">
			<el-button type="primary" size="medium" @click="addBtnReset" style="margin: 0 20px;">新增</el-button>
    </el-col>

    <!--列表-->
		<el-table :data="roleList" highlight-current-row class="el-table-style">
			<el-table-column type="selection" width="55">
			</el-table-column>
			<el-table-column type="index" width="60" sortable>
			</el-table-column>
      <el-table-column prop="display_name" label="角色名称" width="250" sortable>
			</el-table-column>
      <el-table-column prop="description" label="角色描述" width="250" sortable>
			</el-table-column>
			<el-table-column label="操作" width="150">
        <template slot-scope="scope">
          <el-button type="primary" size="mini" @click="roleSet(scope.row.id)">编辑</el-button>
          <el-button type="danger" class="danger-btn" size="mini" @click="delRole(scope.row.id)">删除</el-button>
        </template>
			</el-table-column>
		</el-table>
		<el-col :span="24" class="footer-bar">
			<!-- <div class="batch"><el-button size="medium ">批量删除</el-button></div> -->
			<div class="page-code">
        <el-pagination 
          background 
          layout="prev, pager, next" 
          :page-size="limit"
          :total="pageTotal"
          @current-change="getRoleList"
        >
        </el-pagination>
      </div>
    </el-col>

    <!-- 新增 -->
		<el-dialog title="新增角色" :visible.sync="addFormVisible" width="300">
			<el-form :model="addForm" label-width="150px" :label-position="labelPosition">
				<el-form-item label="角色名称(开发者用)">
          <el-input v-model="addForm.name" auto-complete="off"></el-input>
				</el-form-item>
        <el-form-item label="角色显示名称">
          <el-input v-model="addForm.displayName" auto-complete="off"></el-input>
				</el-form-item>
				<el-form-item label="角色描述">
          <el-input type="textarea" :rows="2"  v-model="addForm.description" auto-complete="off"></el-input>
				</el-form-item>
				<el-form-item label="角色权限">
          <!-- <el-checkbox>全选</el-checkbox>
          <div style="margin: 15px 0;"></div> -->
          <el-checkbox-group  v-model="addForm.checked">
            <el-checkbox v-for="permission in permissionsList" :label="permission.id" :key="permission.id">{{permission.display_name}}</el-checkbox>
          </el-checkbox-group>
				</el-form-item>
			</el-form>
			<div slot="footer" class="dialog-footer">
				<el-button @click.native="addCancelBtn">取消</el-button>
				<el-button type="primary" @click="addRoleSubmit">提交</el-button>
			</div>
		</el-dialog>

    <!-- 编辑 -->
		<el-dialog title="编辑角色" :visible.sync="editFormVisible" width="300">
			<el-form :model="editForm" label-width="150px" :label-position="labelPosition">
				<el-form-item label="角色名称(开发者用)">
          <el-input v-model="editForm.name" auto-complete="off"></el-input>
				</el-form-item>
        <el-form-item label="角色显示名称">
          <el-input v-model="editForm.displayName" auto-complete="off"></el-input>
				</el-form-item>
        <el-form-item label="角色描述">
          <el-input type="textarea" :rows="2"  v-model="editForm.description" auto-complete="off"></el-input>
				</el-form-item>
				<el-form-item label="角色权限">
          <!-- <el-checkbox>全选</el-checkbox>
          <div style="margin: 15px 0;"></div> -->
          <el-checkbox-group  v-model="editForm.checked">
            <el-checkbox v-for="permission in permissionsList" :label="permission.id" :key="permission.id">{{permission.display_name}}</el-checkbox>
          </el-checkbox-group>
				</el-form-item>
			</el-form>
			<div slot="footer" class="dialog-footer">
				<el-button @click.native="editCancelBtn">取消</el-button>
				<el-button type="primary" @click.native="editRoleSubmit">提交</el-button>
			</div>
		</el-dialog>
  </div>
</template>

<script>
export default {
  data(){
    return{
      labelPosition: 'right',
      permissionsList: [],
      roleList:[],
      addFormVisible: false,//新增界面是否显示
      editFormVisible: false,//编辑界面是否显示
      pageTotal:0,
			limit:16,
      //编辑界面数据
			editForm: {
        id:'',
        name:'',
        displayName:'',
        description:'',
        checked: [],
      },
      //新增界面数据
			addForm: {
				name:'',
        displayName:'',
        description:'',
        checked: [],
      },
      
    }
  },
  created(){
    this.getRoleList();
    this.getPermissionsList();
  },
  methods:{
    //获取权限列表
    async getPermissionsList(){
      const res = await this.$api.getPermissionsList();
      if(res.data.level=='success'){
        let list = res.data.data;      
        this.permissionsList = list;
      }else{
        this.$message({
          message: res.data.message,
          type: 'warning'
        });
      }
		},
    //获取角色列表
    async getRoleList(page){
      let params = {
				limit:this.limit,
				page:page || 1,
			}
      const res = await this.$api.getRoleList(params);
      if(res.data.level=='success'){
        this.roleList = res.data.data;
        this.pageTotal = res.data.page_info.total
      }else{
        this.$message({
          message: res.data.message,
          type: 'error'
        });
      }
    },
    //新增取消按钮
		addCancelBtn(){
      this.addForm.name = "";
			this.addForm.displayName = "";
			this.addForm.permissionList = [];
			this.addForm.checked = [];
			this.addFormVisible = false;
		},
    //编辑取消按钮
		editCancelBtn(){
      this.editForm.name = "";
			this.editForm.displayName = "";
			this.editForm.permissionList = [];
			this.editForm.checked = [];
			this.editFormVisible = false;
		},
		//新增按钮重置表单
		addBtnReset(){
      this.addForm.name = '';
      this.addForm.displayName = '';
      this.addForm.description = '';
			this.addForm.checked = [];
			this.addFormVisible = true;
		},
    //编辑角色
    async roleSet(id){
      const params = {
        id,
      }
      const res = await this.$api.roleSet(params);
      if(res.data.level=='success'){
        this.editFormVisible = true;
        this.editForm.id = res.data.data.id;
        this.editForm.name = res.data.data.name;
        this.editForm.displayName = res.data.data.display_name;
        this.editForm.description = res.data.data.description;
        this.editForm.permissionList = res.data.data.permission_id_list;
        let arr = res.data.data.permission_id_list;
        let checkedArr = [];
        arr.forEach((item,index)=>{
          checkedArr.push(item.id)
        })
        this.editForm.checked = checkedArr;
      }else{
        this.$message({
          message: res.data.message,
          type: 'warning'
        });
      }
    },
    //编辑提交按钮
		async editRoleSubmit(){
			const params = {
				id:this.editForm.id,
				name:this.editForm.name,
				display_name:this.editForm.permissionName,
        description:this.editForm.description,
        permission_ids:this.editForm.checked,
      }
      const res = await this.$api.setRole(params);
      if(res.data.level=='success'){
				this.$message({
          message: '修改成功',
          type: 'success'
        });
        this.editCancelBtn();
				this.getPermissionsList();
      }else{
        this.$message({
          message: res.data.message,
          type: 'warning'
        });
      }
    },
    //新增提交按钮
		async addRoleSubmit(){
			const params = {
				name:this.addForm.name,
				display_name:this.addForm.displayName,
        description:this.addForm.description,
        permission_ids:this.addForm.checked,
			}
      const res = await this.$api.setRole(params);
      if(res.data.level=='success'){
				this.$message({
          message: '添加成功',
          type: 'success'
        });
        this.addCancelBtn();
				this.getRoleList();
      }else{
        this.$message({
          message: res.data.message,
          type: 'warning'
        });
      }
		},
		//删除角色
		async delRole(id){
			const params = {
        id:id,
			}
			//删除
			const judge= this.$confirm('删除该权限?', '提示', {
				confirmButtonText: '确定',
				cancelButtonText: '取消',
				type: 'warning'
				}).then(async() => {
					let res = await this.$api.delRole(params)
					if(res.data.level=='success'){
						this.getRoleList();
						this.$message({
							type: 'success',
							message: res.data.message
						});
					}else{
						this.$message({
							type: 'error',
							message: res.data.message
						});
					}
				}).catch(() => {
			});
		}
	}
}
</script>
<style lang='scss' scoped>
  .character{
    .action-bar{
			background-color: #f2f2f2;
			height: 60px;
      margin: 10px 0px;
      box-sizing: border-box;
			display: flex;
			align-items: center;
			padding-left: 20px;
			.action-bar-input{
				width: 250px;
				margin-right: 14px;
			}
		}
		.el-table-style{
			width: 100%;
			border: 1px solid #f2f2f2;
		}
		.footer-bar{
			display: flex;
			justify-content: space-between;
			align-items: center;
      background: #f2f2f2;
      padding: 8px;
      margin: 10px 0px;
      box-sizing: border-box;
		}
		.dialog-footer{
			text-align: center;
		}
  }
</style>

<style>
	.el-pagination.is-background .el-pager li,
	.el-pagination.is-background .btn-prev,
	.el-pagination.is-background .btn-next
	{
		background-color: #fff;
		border: 1px solid #f1f1f1;
		margin: 0;
	}
  .member .el-dialog__body{overflow: auto;}
    .avatar-uploader .el-upload {
    border: 1px dashed #d9d9d9;
    border-radius: 6px;
    cursor: pointer;
    position: relative;
    overflow: hidden;
  }
  .avatar-uploader .el-upload:hover {
    border-color: #409EFF;
  }
  .avatar-uploader-icon {
    font-size: 28px;
    color: #8c939d;
    width: 178px;
    height: 178px;
    line-height: 178px;
    text-align: center;
  }
  .avatar {
    width: 178px;
    height: 178px;
    display: block;
  }
</style>